//**************************************************//
// Unique Keychain Arduino Code                     //
// ECE 387, Miami University, Spring 2013           // 
// Created By: Andrew Heldt, Lee Mondini, and       //
// Shiloh Womack                                    //
//**************************************************//
int emailPin = 13; // Output connected to digital pin 13
int outPin = 12; // Output connected to digital pin 12
int inPin = 7; // Input connected to digital pin 4
int pressed = 0; // Has the button been pressed
int mail = HIGH; // Is there new mail?
int val; // Value read from the serial port

void setup()
{
 pinMode(outPin, OUTPUT); // sets the digital pin as output
 pinMode(inPin, INPUT); // sets the digital pin as output
 pinMode(emailPin, OUTPUT); // sets the digital pin as output
 Serial.begin(9600);
 Serial.flush();
 mail = LOW; // start off with lights out
}

void loop()
{
  pressed = digitalRead(inPin);
  
  if(pressed == LOW)
  {
      digitalWrite(outPin, HIGH);
  }
  else
  {
      digitalWrite(outPin, LOW);
  }
  
   // Read from serial port
 if (Serial.available())
 {
   val = Serial.read();
   Serial.write(val);
    
   if (val == 110) // n = 110 in dec
   {
     mail = HIGH;  // HIGH is off because led is connected to +5v on the other side
   }

   else if (val == 109) //109 = m in dec
   {
     mail = LOW; // LOW is on because led is connected to +5v on the other side    
   }
 }
 
 // Set the status of the output pin
 digitalWrite(emailPin, mail);

}
